//
//  MainViewController.swift
//  新浪微博
//
//  Created by MBP on 16/5/10.
//  Copyright © 2016年 MBP. All rights reserved.
//

import UIKit

class MainViewController: UITabBarController {

    override func viewDidLoad() {
        super.viewDidLoad()
        //添加子控制器
        addChildViewControllers()
        //设置tabbar
        setupTabbar()
        
        //设置新特性
        showNewFeature()
        
    }
    override func viewWillAppear(animated: Bool) {
        super.viewWillAppear(animated)
        //将composeButton置于顶层
        tabBar.bringSubviewToFront(composeButton)
        
        let width = (tabBar.bounds.width / CGFloat(childViewControllers.count)) - 1
        composeButton.frame = CGRectInset(tabBar.bounds, width * 2, 0)
    }

    private lazy var composeButton:UIButton = {
        let button = UIButton()
        let imageName = "tabbar_compose_icon_add"
        button.setImage(UIImage(named: imageName), forState: .Normal)
        button.setImage(UIImage(named: imageName + "_highlighted"), forState: .Highlighted)
        
        let backImageName = "tabbar_compose_button"
        button.setBackgroundImage(UIImage(named: backImageName), forState: .Normal)
        button.setBackgroundImage(UIImage(named: backImageName + "_highlighted"), forState: .Highlighted)
    
        button.sizeToFit()
        return button
    }()
}

// 新特性部分
extension MainViewController {
    private func showNewFeature() {
        //如果没有登录, 直接返回
        if !UserAccount.sharedUserAccount.isLogin {
            return
        }
        
        //已经登录, 判断是否有新版本
        let subjionView = isNewVersion ? NewFeature() : WelcomeView()
        
        //添加到Main控制器里
        view.addSubview(subjionView)
    }
    
    private var isNewVersion: Bool {
        // 1 获取当前版本
        let currentVersion = NSBundle.pn_currentVersion
        
        // 2 获取上一次的版本
        let versionKey = "pn_versionKey"
        let sandboxVersion = NSUserDefaults.standardUserDefaults().stringForKey(versionKey)
        
        // 3 将当前版本保存到偏好
        NSUserDefaults.standardUserDefaults().setObject(currentVersion, forKey: versionKey)
        NSUserDefaults.standardUserDefaults().synchronize()
        
        //输出比较结果(因为上新版本时, 版本号不允许降低, 所以 "!=" 即可,也可用 ">")
        print("是否新版本 \(currentVersion != sandboxVersion)")
        
        // 4 返回当前版本与上一个版本的比较结果
        return currentVersion != sandboxVersion
    }
    
    
}

// 设置tabbar
extension MainViewController {
    
    private func setupTabbar() {
        tabBar.shadowImage = UIImage.cz_singleDotImage(UIColor(white: 0.9, alpha: 1.0))
        tabBar.backgroundImage = UIImage(named: "tabbar_background")
        //发布按钮
        tabBar.addSubview(composeButton)
        composeButton.addTarget(self, action: #selector(MainViewController.clickComposeButton), forControlEvents: .TouchUpInside)
    }
    @objc private func clickComposeButton() {
        print("点击了发布按钮")
    }
    
    
}

//添加子控制器
extension MainViewController {
    private func addChildViewControllers() {
        //首页控制器
        addChildViewController(HomeViewController(), title: "首页", imageName: "tabbar_home")
        //消息控制器
        addChildViewController(MessageViewController(), title: "消息", imageName: "tabbar_message_center")
        //占位控制器
        addChildViewController(UIViewController())
        //发现控制器
        addChildViewController(DiscoverViewController(), title: "发现", imageName: "tabbar_discover")
        //更多控制器
        addChildViewController(ProfileViewController(), title: "我", imageName: "tabbar_profile")
        
    }
    
    private func addChildViewController(vc: UIViewController, title: String, imageName: String) {
        //设置tabbar属性
        vc.title = title
        vc.tabBarItem.setTitleTextAttributes([NSFontAttributeName: UIFont.systemFontOfSize(10)], forState: .Normal)
        vc.tabBarItem.image = UIImage(named: imageName)
        vc.tabBarItem.selectedImage = UIImage(named: imageName + "_selected")?.imageWithRenderingMode(.AlwaysOriginal)
        
        let nav = UINavigationController(rootViewController: vc)
        addChildViewController(nav)

    }
        
    
}
